#!/bin/sh

echo "###############################################################"
echo "### 新统一支付平台(NUPP)停止脚本 "
echo "###############################################################"

APPPATH=/home/gpp/

stop_one_app(){	
  # 将所传入参数首字母大写，用于调用对应的脚本
  app_name=$(echo "$1" | sed 's/\b[a-z]/\u&/')
  echo ""

  # 执行停止命令
  $APPPATH/app/nupp-$1/stop${app_name}.sh

  if [ $? == 0 ]; then
    echo "nupp-$1 server stop success..."
  fi
}

stop_apigateway_app(){	
  echo ""

  # 执行停止命令
  $APPPATH/app/nupp-api-gateway/stopApigateway.sh

  if [ $? == 0 ]; then
    echo "nupp-api-gateway server stop success..."
  fi
}

stop_nginx(){	
  echo "preparing to stop nginx ..."

  # 执行停止nginx命令
  $APPPATH/dev/nginx/stopNg.sh
  if [ $? == 0 ]; then
    echo "nginx server stop success..."
  fi
}

usage() {
  echo "用法: $0 [参数]"
  echo "参数:"
  echo "    all          停止所有的应用程序包"
  echo "    nginx        停止nginx服务"
  echo "    settings     停止服务nupp-settings    程序包"
  echo "    notice       停止服务nupp-notice      程序包"
  echo "    security     停止服务nupp-security    程序包"
  echo "    gateway      停止服务nupp-gateway     程序包"
  echo "    routing      停止服务nupp-routing     程序包"
  echo "    channel      停止服务nupp-channel     程序包"
  echo "    business     停止服务nupp-business    程序包"
  echo "    checking     停止服务nupp-checking    程序包"
  echo "    workflow     停止服务nupp-workflow    程序包"
  echo "    scheduler    停止服务nupp-scheduler   程序包"
  echo "    position     停止服务nupp-position    程序包"
  echo "    api-gateway  停止服务nupp-api-gateway 程序包"

}

#### MAIN ####
if [ $# == 0 ]; then
  usage
elif [ $# == 1 ]; then
  case $1 in 
    "channel"|"business"|"checking"|"workflow"|"scheduler"|"analysis"|"notice"|"position"|"settings"|"routing"|"gateway"|"security")
      stop_one_app $1
      ;;
    "api-gateway")
      stop_apigateway_app
      ;;	  
    "nacos")
      stop_nacos
      ;;	  
    "nginx")
      stop_nginx
      ;;
    "all")
      # 停止所有程序
      stop_one_app "channel"
      stop_one_app "business"
      stop_one_app "checking"
      stop_one_app "workflow"
      stop_one_app "scheduler"
      stop_one_app "analysis"
      stop_one_app "notice"
      stop_one_app "position"
      stop_one_app "settings"
      stop_one_app "routing"
      stop_one_app "gateway"
      stop_one_app "security"
      stop_apigateway_app "api-gateway"
      stop_nginx
      ;;	  
    *)
      echo "invalid parameter [$1]..."
      ;;	  
  esac
fi



